<?php
namespace Act\Os\Payment;
use Act\Os\Backend;
class Config extends Backend{
	public function doGet(){
		$code = get('code','','trim');
		$this->checkParam($code);
		$payment_cls = '\\Lib\\Payment\\'.$code.'\\'.$code;
		if(!class_exists($payment_cls)){
			return $this->error('该支付接口不存在',referer());
		}
		//原有配置
		$payment = model('#Payment')->getPaymentByCode($code);
		$payment['config'] = unserialize($payment['config']);
		$_POST = $payment;
		$config_field = $payment_cls::getConfigField();
		$this->display('form.php',[
			'config_field'=>$config_field,
		]);
	}

	public function doPost(){
		$code = get('code','','trim');
		$this->checkParam($code);
		$payment_cls = '\\Lib\\Payment\\'.$code.'\\'.$code;
		if(!class_exists($payment_cls)){
			return $this->error('该支付接口不存在',referer(referer()));
		}
		$payment_mod = model('#Payment');
		$data = [
			'payment_code'=>$code,
			'payment_name'=>$payment_cls::getName(),
			'config'=>serialize(post('config')),
			'enabled'=>1,
			'sort_order'=>post('sort_order',255,'absint'),
		];
		if($payment_mod->getPaymentByCode($code)){
			$ret = $payment_mod->updatePayment($code,$data);
		}else{
			$ret = $payment_mod->addPayment($data);
		}
		if($ret!==false){
			return $this->success('操作成功','/os/payments');
		}

		$this->alert($payment_mod->getError());
		$config_field = $payment_cls::getConfigField();
		$this->display('form.php',[
			'config_field'=>$config_field,
		]);
	}
}